package com.example.cdctest.managecontroller;

import com.example.cdctest.pojo.Admin;
import com.example.cdctest.pojo.Knowledge;
import com.example.cdctest.service.KnowledgeService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/manage")
public class ManageKnowledgeController {

    @Resource
    private KnowledgeService knowledgeService;

    //取出所有疾控知识
    @RequestMapping("knowledge")
    public ModelAndView Knowledge(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("knowledgeList",knowledgeService.getKnowledge());
        modelAndView.setViewName("/manage/knowledge");
      return modelAndView;

    }
    //停用启用疾控知识
    @RequestMapping("updateEnable")
    @ResponseBody
    public void updateEnable(Integer id)
    {
       knowledgeService.updateEnable(id);
    }

    @RequestMapping("knowledge-add")
    public void KnowledgeAdd(){    }

//添加疾控知识
    @RequestMapping("knowledge-add-form")
    public String KnowledgeAddForm(Knowledge knowledge, HttpSession session, @RequestParam("file") MultipartFile image) {
        Admin admin = (Admin) session.getAttribute("loginAdmin");
        if(!image.isEmpty()) {
            String ext = FilenameUtils.getExtension(image.getOriginalFilename());
            if (!",jpg,jpeg,png,gif,".contains("," + ext + ",")) {
                return "redirect:/manage/knowledge-add";
            }
            String fileName = UUID.randomUUID() + "." + ext;
            File directory = new File("src/main/webapp/knowledge/image");
            String path = null;
            try {
                path = directory.getCanonicalPath();
            } catch (IOException e) {
                e.printStackTrace();
            }
            File file1 = new File(path, fileName);
            try {
                image.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            knowledge.setImage(fileName);
            knowledge.setAdmin(admin);
        }
        knowledgeService.knowledgeAdd(knowledge);
        return "/manage/knowledge-add";
    }
    //编辑疾控知识的答案
    @RequestMapping("updatedaan")
    @ResponseBody
    public void updatedaan(Integer id,String newVals)
    {
        Knowledge knowledge=new Knowledge();
        knowledge.setId(id);
        knowledge.setAnswer(newVals);
        knowledgeService.updatedaan(knowledge);

    }
    //删除疾控知识
    @RequestMapping("deleteKnowledge")
    @ResponseBody
    public void deleteKnowledge(Integer id)
    {
        knowledgeService.deleteKnowledge(id);
    }

}